package Day004.demo004_Calendar类;

/*
    Calender类:
                    概述:java.util 包  表示日历的抽象类 不能创建对象,只能创建其子类对象
                            public abstract class Calender extends Object

                    获取当前系统日历对象的方式:
                                    1.通过Calender类的子类: GregorianCalendar类   日历类
                                                          GregorianCalendar gc = new GregorianCalendar();
                                    2.通过Calender类的静态方法:
                                                          public static Calender getInstance();
                                                          Calendar cal = Calender.getInstance();
                    常用方法:
                            1.public int get(int field)     获取某个字段的值
                                Calender.YEAR  年字段
                                Calender.MONTH 月字段
                                Calender.DAY_OF_MONTH 日字段
                                Calender.HOUR_OF_DAY  时字段
                                Calender.MINUTE  分钟字段
                                Calender.SECOND  分钟字段
                                Calender.MILLISECOND  分钟字段

                            2.public void set(int field,int value)    设置某个字段的值
                                cal.set(Calendar.YEAR,2002);

                            3.public void add(int field,int amount)   为某个字段增加/减少指定的值
                                cal.add(Calendar.YEAR,2);

                            扩展
                            4.void setTime(Date date)  使给定的Date设置此Calendar时间

                            5.boolean before(Object when)  判断两个日历对象谁在前

 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Test {
    public static void main(String[] args) {

        GregorianCalendar gc = new GregorianCalendar();
        System.out.println("日历对象:" + gc);

        Calendar cal = Calendar.getInstance();
        System.out.println("日历对象:" + cal);

        //获取Calender类对象
        Calendar cal1 = Calendar.getInstance(); //当前系统时间的日历对象
        System.out.println(cal1);
        /*
        java.util.GregorianCalendar[time=1587276316900,areFieldsSet=true,areAllFieldsSet=true,lenient=true,
        zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,
        transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=3,
        WEEK_OF_YEAR=17,WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=110,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,
        AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=5,SECOND=16,MILLISECOND=900,ZONE_OFFSET=28800000,DST_OFFSET=0]

         */
        //1. public int get(int field)  获取某个字段的值
        //获取年
        int year = cal1.get(Calendar.YEAR);
        System.out.println(year); // 2020

        int year1 = gc.get(Calendar.YEAR);
        System.out.println(year1);

        //获取月
        int month = cal1.get(Calendar.MONTH);
        System.out.println(month); // 3  0-11对应1-12月
        //获取日
        System.out.println(cal1.get(Calendar.DAY_OF_MONTH));// 19
        //获取星期
        System.out.println(cal1.get(Calendar.DAY_OF_WEEK));// 1   1-7对应周日到周六
        //获取时
        System.out.println(cal1.get(Calendar.HOUR_OF_DAY));// 14
        //获取分
        System.out.println(cal1.get(Calendar.MINUTE));
        //获取秒
        System.out.println(cal1.get(Calendar.SECOND));
        //获取毫秒
        System.out.println(cal1.get(Calendar.MILLISECOND));
        System.out.println("---------------------------------------------------");

        //2. public void set(int field,int value)  设置某个字段的值
        cal1.set(Calendar.YEAR,2002);
        cal1.get(Calendar.YEAR);
        System.out.println("年:" + cal1.get(Calendar.YEAR));

        //3. public void add(int field,int amount)  为某个字段增加/减少指定的值
        cal1.add(Calendar.YEAR,2);
        //gc.add(Calendar.YEAR,2);
        cal1.get(Calendar.YEAR);
        System.out.println("增加后的年:"+cal1.get(Calendar.YEAR)); // 2004

        //4. void setTime(Date date)  使给定的Date设置此Calendar时间
        //获取标准基准时间对应的日历对象
        Date date = new Date(0);
        cal1.setTime(date);
        System.out.println(cal1);

        //5. boolean before(Object when)  判断两个日历对象谁在前
        Calendar cal2 = Calendar.getInstance();//静态方法创建对象
        Calendar cal3 = Calendar.getInstance();
        cal3.setTime(date);//标准基准时间日历对象
        //比较
        System.out.println(cal2.before(cal3)); //false
        System.out.println(cal3.before(cal2)); //true
    }
}
